/*
 * Copyright ©2015-2021 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.jaemon.spring.adorner.type;

import io.gitee.jaemon.spring.adorner.annotation.GroupNote;
import io.gitee.jaemon.spring.adorner.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMethod;

import java.lang.reflect.Method;
import java.util.Comparator;

/**
 * Api定义对象
 *
 * @author Jaemon
 * @since 1.0
 */
public class ApiDefinition<T> {
    /** controller类 */
    private Class<?> clazz;
    /** 接口对应方法对象 */
    private Method method;
    /** api接口所在controller类名称, 分组名称 {@link GroupNote} */
    private String groupName;
    /** api接口名称 {@link io.gitee.jaemon.spring.adorner.annotation.ApiNote} */
    private String apiName;
    /** 接口地址 */
    private String[] urls;
    /** 请求类型 */
    private RequestMethod[] requestMethods;
    /** 请求体 */
    private T request;
    /** 响应体 */
    private T response;
    /**
     * 序号
     */
    private int order;

    public ApiDefinition(
            Class<?> clazz, Method method,
            String groupName, String apiName,
            String[] urls, RequestMethod[] requestMethods,
            T request, T response, int order
    ) {
        this.clazz = clazz;
        this.method = method;
        this.groupName = groupName;
        this.apiName = apiName;
        this.urls = urls;
        this.requestMethods = requestMethods;
        this.request = request;
        this.response = response;
        this.order = order;
    }

    public static <T> ApiDefinition of(
            Class<?> clazz, Method method,
            String groupName, String apiName,
            String[] urls, RequestMethod[] requestMethods,
            T request, T response, int order
    ) {
        return new ApiDefinition(clazz, method, groupName, apiName, urls, requestMethods, request, response, order);
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public Method getMethod() {
        return method;
    }

    public String getGroupName() {
        if (StringUtils.isEmpty(groupName)) {
            return clazz.getSimpleName();
        }
        return groupName;
    }

    public String getApiName() {
        if (StringUtils.isEmpty(apiName)) {
            return method.getName();
        }
        return apiName;
    }

    public String[] getUrls() {
        return urls;
    }

    public String getDefaultUrl() {
        return urls[0];
    }

    public RequestMethod[] getRequestMethods() {
        return requestMethods;
    }

    public RequestMethod getDefaultRequestMethod() {
        return requestMethods[0];
    }

    public T getRequest() {
        return request;
    }

    public T getResponse() {
        return response;
    }

    public int getOrder() {
        return order;
    }
}